// pages/empower/empower.js
var app = getApp()
import {
  bindphone,
  decrypt,
  getcode
} from "../../api/index.js"
const regx = /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/
Page({

  /**
   * 页面的初始数据
   */
  data: {
    issendcode: true,
    phone:'',
    code:'',
    openid:'',
    TIME_COUNT: 120, // 验证码倒计时60s
    count: '',
    issendcode: true,
  },
  phoneInput(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  getCode() {
    var _this = this;
    if (this.data.phone == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none'
      })
    } else {
      getcode(this.data.phone).then(res => {
        if (res.code == 200) {
          if (!_this.data.timer) {
            _this.setData({
              count: _this.data.TIME_COUNT,
              issendcode: false
            })
            _this.data.timer = setInterval(() => {
              if (_this.data.count > 0 && _this.data.count <= _this.data.TIME_COUNT) {
                _this.setData({
                  count: _this.data.count - 1
                })
              } else {
                clearInterval(_this.data.timer)
                _this.data.timer = null
                _this.setData({
                  issendcode: true
                })
              }
            }, 1000)
          }
          wx.showToast({
            title: '验证码已发送至您的手机，请注意查收~',
            icon: 'none'
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      openid: options.openId
    })
  },
  codeChange(e) {
    this.setData({
      code: e.detail.value
    })
  },
  PhoneNumber() {
    let data = {
      phone: this.data.phone,
      code: this.data.code,
      openid:this.data.openid
    }
    if (this.data.phone == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none'
      })
    } else if (this.data.code == '') {
      wx.showToast({
        title: '请填写短信验证码',
        icon: 'none'
      })
    }
    bindphone(data).then(res => {
      if (res.code == 200) {
        wx.setStorageSync('token', res.token)
        wx.setStorageSync('userInfo', res.user)
        wx.setStorageSync('accessToken', res.tokens.accessToken)
        wx.setStorageSync('refreshToken', res.tokens.refreshToken)
        app.globalData.token = res.token
        app.globalData.userInfo = res.user
        wx.showToast({
          title: '登陆成功！',
          icon: 'none',
          success: function () {
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/home/home',
              })
            }, 2000);
          }
        })
      }else  if(res.code != 200){
        wx.showToast({
          title: res.msg,
          icon: 'none',
        })
        return
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})